package com.quanyue.haircut.shop.service.impl;

import com.quanyue.auto.util.AssertUtils;
import com.quanyue.haircut.domain.entity.Technician;
import com.quanyue.haircut.domain.entity.TechnicianServer;
import com.quanyue.haircut.shop.dto.TechnicianCreate;
import com.quanyue.haircut.shop.dto.TechnicianDetail;
import com.quanyue.haircut.shop.mapper.TechnicianMapper;
import com.quanyue.haircut.shop.mapper.TechnicianServerMapper;
import com.quanyue.haircut.shop.service.TechnicianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class TechnicianServiceImpl implements TechnicianService {
    @Autowired
    private TechnicianMapper technicianMapper;
    @Autowired
    private TechnicianServerMapper technicianServerMapper;

    @Override
    @Transactional
    public Technician createTechnician(TechnicianCreate technicianCreate) {
        Technician technician = technicianCreate.getTechnician();
        technician.setCreateTime(LocalDateTime.now());
        technician.setModifyTime(LocalDateTime.now());
        technician.setDelFlag(false);
        technicianMapper.insert(technician);
        List <TechnicianServer> technicianServers = new ArrayList <>();
        technicianCreate.getServerCreates().forEach(server -> {
            TechnicianServer technicianServer = new TechnicianServer();
            technicianServer.setCreateTime(LocalDateTime.now());
            technicianServer.setModifyTime(LocalDateTime.now());
            technicianServer.setServer(server.getShopServerId());
            technicianServer.setReservePrice(server.getServerPrice());
            technicianServer.setTechnicianId(technician.getId());
            technicianServer.setDelFlag(false);
            technicianServers.add(technicianServer);
        });
        technicianServerMapper.insertAll(technicianServers);
        return technician;
    }

    @Override
    public void updateTechnician(Technician technician) {
        Technician check = technicianMapper.findOne(technician.getId());
        AssertUtils.notNull(check, "技师信息校检异常");
        technician.setCreateTime(check.getCreateTime());
        technician.setModifyTime(LocalDateTime.now());
        technician.setDelFlag(false);
        technicianMapper.updateSelective(technician);
    }

    @Override
    public void deleteTechnician(Integer technicianId) {
        Technician check = technicianMapper.findOne(technicianId);
        AssertUtils.notNull(check, "技师信息校检异常");
        technicianMapper.deleteOne(technicianId);
    }

    @Override
    public TechnicianDetail selectTechnician(Integer technicianId) {
        TechnicianDetail detail = new TechnicianDetail();
        detail.setTechnician(technicianMapper.findOne(technicianId));
        detail.setServerLists(technicianMapper.findTechnicianDetail(technicianId));
        return detail;
    }

    @Override
    public List <TechnicianDetail> technicianList(Integer shopId, Integer serverId) {
        List <TechnicianDetail> details = new ArrayList <>();
        List <Technician> technicians;
        if (serverId != 0) {
            technicians = technicianMapper.findByServer(serverId);
        } else {
            technicians = technicianMapper.findByShopValid(shopId);
        }
        technicians.forEach(technician -> {
            details.add(this.selectTechnician(technician.getId()));
        });
        return details;
    }
}
